# Maintainer: Andrea Zagli <andrea.zagli.free@gmail.com>

_realname=meson
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.58.0
pkgrel=1
pkgdesc="High-productivity build system (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://mesonbuild.com/"
license=("Apache 2")
options=('strip' 'staticlibs')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-ninja")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=("https://github.com/mesonbuild/${_realname}/releases/download/${pkgver}/${_realname}-${pkgver}.tar.gz"
        'color-term.patch'
        '0002-Default-to-sys.prefix-as-the-default-prefix.patch'
        '0004-fix-gtk-doc.patch'
        'install-man.patch')
sha256sums=('f4820df0bc969c99019fd4af8ca5f136ee94c63d8a5ad67e7eb73bdbc9182fdd'
            '5805aed0a117536eb16dd8eef978c6be57c2471b655ede63e25517c28b4f4cf0'
            '675e79a0fc78b4236f5b803ffd2e745cb2ff125c9b4a03657773915183a4e19a'
            '0f9177102976bbcbdf50c1da49842783a0c77be939e839f1bf91dcaba6a5cdee'
            '0682a36cb75e545a78b81293303835a16171f25baf949905dc08029436efff84')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}"/color-term.patch
  patch -Np1 -i "${srcdir}"/0002-Default-to-sys.prefix-as-the-default-prefix.patch
  patch -Np1 -i "${srcdir}"/0004-fix-gtk-doc.patch
  patch -Np1 -i "${srcdir}"/install-man.patch
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  cp -rf "${srcdir}"/${_realname}-${pkgver}/. "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd "${srcdir}"/build-${CARCH}

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py \
    install --prefix=${MINGW_PREFIX} --root="${pkgdir}"

  PREFIX_WIN=$(cygpath -m ${MINGW_PREFIX})
  sed -s "s|${PREFIX_WIN}/bin/||g" \
    -i "${pkgdir}${MINGW_PREFIX}/bin/meson-script.py"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
